package cn.edu.cqytxy.controller;

import cn.edu.cqytxy.entity.Aircraft;
import cn.edu.cqytxy.service.AircraftService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Aircraft 控制器
 *
 * @author yinjw
 */
public class AircraftServlet extends HttpServlet {

    private AircraftService aircraftService = new AircraftService();

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name = req.getParameter("name");
        String num = req.getParameter("num");
        String altitude = req.getParameter("altitude");
        String speed = req.getParameter("speed");

        Aircraft aircraft = new Aircraft();
        aircraft.setName(name);
        aircraft.setNum(Integer.parseInt(num));
        aircraft.setAltitude(Integer.parseInt(altitude));
        aircraft.setSpeed(Integer.parseInt(speed));
        aircraftService.insertAircraft(aircraft);
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String s = aircraftService.selectAll();

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println(s);
    }
}
